//
//  RollView.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RollView.h"

@interface RollView()<UICollectionViewDelegate>

@end

@implementation RollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

{
    NSArray *_imageArray;
}


- (instancetype)initWithFrame:(CGRect)frame imageArr:(NSArray *)arr {
    self = [super initWithFrame:frame];
    if (self) {
        _imageArray = arr;
        
        [self createView];
    }
    return self;
}

- (void)setarr:(NSArray *)arr {
    
    _imageArray = arr;
}

- (void)createView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    flowLayout.itemSize = CGSizeMake(VIEW_WIDTH - 10, VIEW_HEIGHT / 3 - 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.RollCollcetionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT / 3) collectionViewLayout:flowLayout];
    self.RollCollcetionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.RollCollcetionView];
    self.RollCollcetionView.pagingEnabled = YES;
    self.RollCollcetionView.dataSource = self;
    self.RollCollcetionView.delegate = self;
    self.RollCollcetionView.showsHorizontalScrollIndicator = NO;
    
    self.RollCollcetionView.contentSize = CGSizeMake(VIEW_WIDTH * (_imageArray.count + 1), 0);
    self.RollCollcetionView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
    
    [self.RollCollcetionView registerClass:[RollCell class] forCellWithReuseIdentifier:@"Roll"];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imageArray.count + 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Roll" forIndexPath:indexPath];
    
    if (indexPath.item == 0) {
        [cell.imageView sd_setImageWithURL:_imageArray.lastObject];
    } else if (indexPath.item == _imageArray.count + 1) {
        [cell.imageView sd_setImageWithURL:_imageArray.firstObject];
    } else {
        [cell.imageView sd_setImageWithURL:_imageArray[indexPath.item - 1]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.RollDelegate pushViewController:indexPath];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    [self.RollCollcetionView setContentOffset:CGPointMake(self.RollCollcetionView.contentOffset.x + WIDTH, 0) animated:YES];
    if (self.RollCollcetionView.contentOffset.x == VIEW_WIDTH * (_imageArray.count + 1)) {
        self.RollCollcetionView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
        
    } else if (self.RollCollcetionView.contentOffset.x == 0) {
        self.RollCollcetionView.contentOffset = CGPointMake(_imageArray.count * VIEW_WIDTH, 0);
    }
    
}

@end
